var addSongFormObject = new Vue({
    el: '#uploadSongModal',
    data: {
        name1: '默认为文件名',
        singer1: 'default',
        lyric1: '',
        style1: '',
        language1: '',
        rank1: 0,
        uploading: false,
    },
    methods: {
        uploadSong: function (fcf) {
            var formData = new FormData();
            formData.append("file", document.getElementById('songFile1').files[0]);
            formData.append("image", document.getElementById('imageFile1').files[0]);
            formData.append("name", addSongFormObject.name1);
            formData.append("singer", addSongFormObject.singer1);
            formData.append("lyric", addSongFormObject.lyric1);
            formData.append("style", addSongFormObject.style1);
            formData.append("language", addSongFormObject.language1);
            formData.append("rank", addSongFormObject.rank1);
            addSongFormObject.uploading = true;
            $.ajax({
                url: '/song/upload',
                type: 'POST',
                cache: false,
                data: formData,
                processData: false,
                contentType: false
            }).done(function (result) {
                switch (result['result']) {
                    case 'done':
                        alert('upload sucess!');
                        break;
                    case 'error':
                        alert('upload failed!');
                        break;
                    case 'permission error':
                        alert('permission error!');
                        break;
                    case 'duplicated song name':
                        alert('song name duplicated!')
                        break;
                    default:
                        break;
                }
                addSongFormObject.uploading = false;

            }).fail(function (result) {
                alert('upload failed!');
                addSongFormObject.uploading = false;
            });
        },
        clear: function () {

        }
    }
});

var webUserTbableObject = new Vue({
    el:"#user",
    data:{
        queryUrl: '/user/get/all/page/',
        users:[],
        titles: ['#', '账号', '等级', '积分', '删除'],
        currentPage: 1,
        pageSize: 0,
        selectedPK: 0,
        selectedName: ""
    },
    methods:{
        getData: function (page) {
            page--;
            var size = 10;
            var path = this.queryUrl + page + '/size/' + size;
            $.get(path, function (data) {
                webUserTbableObject.users = data['user'];
                webUserTbableObject.pageSize = data['pages'];
                webUserTbableObject.currentPage = data['current'];
            });
        },
        updateWebUserMark: function (pk, mark) {
            webUserTbableObject.selectedPK = pk;
            webUserTbableObject.selectedName = mark;
            console.log(pk+" "+mark);
        },
        updateMark:function () {
            var user = {
                pk:webUserTbableObject.selectedPK,
                points:webUserTbableObject.selectedName
            }
            $.post('/user/update/1',user,function (result) {
                console.log(result);
                if (result['result'] == 'success') {
                    alert('更新成功');
                }else {
                    alert("更新失败");
                }
                location.href = "/admin/page";
            });
        },
        updateWebUserRank: function (pk, rank) {
            console.log(pk+" "+rank);
            webUserTbableObject.selectedPK = pk;
            webUserTbableObject.selectedName = rank;
        },
        updateRank:function () {
            var user = {
                pk:webUserTbableObject.selectedPK,
                rank:webUserTbableObject.selectedName
            }
            $.post('/user/update/2',user,function (result) {
                console.log(result);
                if (result['result'] == 'success') {
                    alert('更新成功');
                }else {
                    alert("更新失败");
                }
                location.href = "/admin/page";
            });
        },
    },
    mounted: function () {
        console.log("mounted.");
        this.getData(1);
    },

});


var loginModel = new Vue({
    el: '#loginModel',
    data: {
        num: "",
        pwd: "",
        isFailed: false,
        hiddenSpan: true,
        warning: '登录失败'
    },
    methods: {
        login: function () {
            loginData = {
                number: loginModel.num,
                password: loginModel.pwd
            }
            $.post('/login', loginData, function (result) {
                switch (result['result']) {
                    case 'success':
                        location.href = '/';
                        break;
                    case 'failed':
                        loginModel.hiddenSpan = false;
                        loginModel.isFailed = true;
                        break;
                    default:
                        break;
                }
            });
        }
    }
});

var songsTableObject = new Vue({
    el: '#songsTableObject',
    data: {
        queryUrl: '/song/get/all/page/',
        songs: [],
        titles: ['#', '歌曲名', '歌手', '删除', '编辑'],
        currentPage: 1,
        pageSize: 0,
        selectedPK: 0,
        selectedName: "",
        selecteSinger:"",
        selecteLanguage:"",
        selecteStyle:"",
        selecteLyric:""

    },
    methods: {
        prePage: function () {
            this.getData(this.currentPage - 1);
        },
        nextPage: function () {
            this.getData(this.currentPage + 1);
        },
        updateSongs: function (newPage) {
            console.log('query for new page: ' + newPage);
            this.getData(newPage);
        },
        updateDeleteModal: function (pk, name) {
            songsTableObject.selectedPK = pk;
            songsTableObject.selectedName = name;
        },
        updateSongInformation: function(pk,name,singer,lyric,style,language){
            songsTableObject.selectedPK = pk;
            songsTableObject.selectedName = name;
            songsTableObject.selecteSinger = singer;
            songsTableObject.selecteLyric = lyric;
            songsTableObject.selecteStyle = style;
            songsTableObject.selecteLanguage = language;
        },
        getData: function (page) {
            page--;
            var size = 10;
            var path = this.queryUrl + page + '/size/' + size;
            $.get(path, function (data) {
                songsTableObject.songs = data['songs'];
                songsTableObject.pageSize = data['pages'];
                songsTableObject.currentPage = data['current'];
            });
        }
    },
    mounted: function () {
        console.log("mounted.");
        this.getData(1);
    },
});

